package com.csg.ses.service.ses.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.csg.ses.common.utils.CommonUtil;
import com.csg.ses.common.utils.MybatisPlusUtil;
import com.csg.ses.common.utils.SnowFlakeUtil;
import com.csg.ses.dao.ses.SysParamTypeDao;
import com.csg.ses.entity.ses.SysParamTypeEntity;
import com.csg.ses.service.ses.SysParamTypeService;
import com.csg.ses.vo.common.DropData;
import com.csg.ses.vo.common.IdListVo;
import com.csg.ses.vo.common.Result;
import com.csg.ses.vo.common.ResultStatus;
import com.csg.ses.vo.login.UserContext;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

/**
 * 系统参数分类
 * @author wangjun
 * @date 2020/11/16
 */
@Service
@RequiredArgsConstructor
public class SysParamTypeServiceImpl extends ServiceImpl<SysParamTypeDao, SysParamTypeEntity> implements SysParamTypeService {
    private final SysParamTypeDao sysParamTypeDao;

    @Override
    public Result retrieve(SysParamTypeEntity type, Long current, Long size) {
        try {
            Page<SysParamTypeEntity> page = MybatisPlusUtil.initPage(current, size, true);
            QueryWrapper<SysParamTypeEntity> qw = new QueryWrapper();
            if (!CommonUtil.isEmpty(type.getParamTypeCode())) {
                qw.like("param_type_code", type.getParamTypeCode());
            }
            if (!CommonUtil.isEmpty(type.getParamTypeName())) {
                qw.like("param_type_name", type.getParamTypeName());
            }
            if (!CommonUtil.isEmpty(type.getUseFlag())) {
                qw.eq("use_flag", type.getUseFlag());
            }
            qw.orderByAsc("id");
            return new Result(sysParamTypeDao.retrieve(page, qw), ResultStatus.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException();
        }
    }

    @Override
    public Result dropData() {
        try {
            QueryWrapper<SysParamTypeEntity> qw = new QueryWrapper();
            qw.eq("use_flag", "Y");
            qw.orderByAsc("id");
            List<SysParamTypeEntity> result = sysParamTypeDao.selectList(qw);

            List<DropData> dropList = new ArrayList<DropData>();
            if (result != null && result.size() > 0) {
                for (SysParamTypeEntity type : result) {
                    DropData drop = new DropData();
                    drop.setValue(type.getParamTypeCode());
                    drop.setLabel(type.getParamTypeName());
                    dropList.add(drop);
                }
            }
            return new Result(dropList, ResultStatus.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException();
        }
    }

    @Override
    @Transactional
    public Result del(IdListVo idList) {
        try {
            if (idList != null && idList.getIdList().size() > 0) {
                List<String> list = idList.getIdList();
                Integer count = sysParamTypeDao.countParams(list);
                if (count > 0) {
                    return new Result("所选参数类型已被引用，不可删除！", ResultStatus.BIZ_FAILED);
                } else {
                    int num = sysParamTypeDao.deleteBatchIds(idList.getIdList());
                    return new Result(num + "条记录删除成功！", ResultStatus.SUCCESS);
                }
            } else {
                return new Result("未指定需要删除的记录！", ResultStatus.BIZ_FAILED);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException();
        }
    }

    @Override
    public Result getById(String id) {
        if (!CommonUtil.isEmpty(id)) {
            return new Result(sysParamTypeDao.selectById(id), ResultStatus.SUCCESS);
        } else {
            throw new RuntimeException("id不能为空！");
        }
    }

    @Override
    @Transactional
    public boolean save(SysParamTypeEntity paramType) {
        try {
            paramType.setModifiedBy(UserContext.getUserView().getLoginCode());
            paramType.setModifyTime(new Timestamp(System.currentTimeMillis()));
            if (!CommonUtil.isEmpty(paramType.getId())) {
                sysParamTypeDao.updateById(paramType);
            } else {
                paramType.setId(SnowFlakeUtil.nextStringId());
                paramType.setUseFlag("Y");
                paramType.setCreatedBy(UserContext.getUserView().getLoginCode());
                paramType.setCreateTime(new Timestamp(System.currentTimeMillis()));
                sysParamTypeDao.insert(paramType);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException();
        }
    }
}